package cn.edu.neu.studentmsg_java.controller.workbench;

import cn.edu.neu.studentmsg_java.cons.R;
import cn.edu.neu.studentmsg_java.cons.State;
import cn.edu.neu.studentmsg_java.model.Course;
import cn.edu.neu.studentmsg_java.model.Selection;
import cn.edu.neu.studentmsg_java.service.CourseService;
import cn.edu.neu.studentmsg_java.service.SelectionService;
import cn.edu.neu.studentmsg_java.service.StudentService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.fasterxml.jackson.databind.util.JSONPObject;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/workbench/selection")
public class SelectionController {

    @Autowired
    private SelectionService selectionService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentService studentService;

    //分别获取主修课和选修课的课程编号和课程名称
    @RequestMapping("/getData")
    public R getData() {
        List<String> minorCourseNameList = courseService.findMinorCourseName();
        if (ObjectUtils.isNotEmpty(minorCourseNameList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(minorCourseNameList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }



    //通过学生编号，获取某个学生的主修课信息或者选修课信息
    @RequestMapping("/findSelectionPartByStudentId")
    public R findSelectionPartByStudentId(Selection selection) {
        //判断学号不为空
        if (selection.getStudent_id() != null) {
            //判断课程类型为1或2
            if (selection.getCategory() == 1 || selection.getCategory() == 2) {
                //通过学生编号去查询选课信息
                List<Selection> courseList = selectionService.findSelectionPartByStudentId(selection);
                if (ObjectUtils.isNotEmpty(courseList)) {
                    return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(courseList).build();
                }
                return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
            }
            return R.builder().code(State.MUST_FIELD_VALUE_MISTAKE.getCode()).msg(State.MUST_FIELD_VALUE_MISTAKE.getMsg()).build();
        }
        return R.builder().code(State.MUST_FIELD_NON_EXISTENT.getCode()).msg(State.MUST_FIELD_NON_EXISTENT.getMsg()).build();
    }

    //新增学生的主修课或选学课的信息
    @RequestMapping("/addSelectionByStudentId")
    public R addSelectionByStudentId(@RequestBody Selection selection) {
        //判断Student_id不为空
        if (ObjectUtils.isNotEmpty(selection.getStudent_id())) {
            //通过学生id添加选修课
            int result = selectionService.addSelectionByStudentId(selection);
            if (result > 0) {
                return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
            }
            return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
        }
        return R.builder().code(State.MUST_FIELD_NON_EXISTENT.getCode()).msg(State.MUST_FIELD_NON_EXISTENT.getMsg()).build();
    }

    //删除学生的主修课或选学课的信息
    @RequestMapping("/removeSelectionByCourseIdAndStudentId")
    public R removeSelectionByCourseIdAndStudentId(Selection selection) {
        //通过学生id和课程id去删除选课表中的信息，目的将该学生和课程关联删除，达到删除选课的目的
        int result = selectionService.removeSelectionByCourseIdAndStudentId(selection);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_DELETE_ERROR.getCode()).msg(State.DB_DELETE_ERROR.getMsg()).build();
    }

    //更新成绩
    @RequestMapping("/modifySelectionFinalScore")
    public R modifySelectionFinalScore(@RequestBody Selection selection) {
        //修改选课表中的学生成绩信息
        int result = selectionService.modifySelectionFinalScore(selection);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_UPDATE_ERROR.getCode()).msg(State.DB_UPDATE_ERROR.getMsg()).build();
    }
}